﻿using CTM.Web.Models.Input;
using CTM.Web.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace CTM.Web.Models.View.Comment
{
    public class CommentViewModel
    {
        #region Properties

        public Guid Id { get; set; }
                
        [Required(ErrorMessage = "*")]
        public string Content { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime CreatedOn { get; set; }

        [StringLength(128)]
        public string CreatedBy { get; set; }

        public DateTime? ModifiedOn { get; set; }

        [StringLength(128)]
        public string ModifiedBy { get; set; }

        public Guid PostId { get; set; }
        public ApplicationUser CreatedUser { get; set; }
        public ApplicationUser ModifiedUser { get; set; }

        #endregion

        #region Relationship
        public virtual Post Post { get; set; }
        public virtual ICollection<Input.Notification> Notifications { get; set; }
        #endregion

        public CommentViewModel()
        {
            IsDeleted = false;
            CreatedOn = DateTime.UtcNow;
            Notifications = new HashSet<Input.Notification>();
        }

        public CommentViewModel(Input.Comment comment)
        {
            Id = comment.Id;
            Content = comment.Content;
            IsDeleted = comment.IsDeleted;
            CreatedOn = ApplicationHelpers.ConvertToLocalTime(comment.CreatedOn);
            CreatedBy = comment.CreatedBy;
            ModifiedOn = comment.ModifiedOn.HasValue ? ApplicationHelpers.ConvertToLocalTime(comment.ModifiedOn.Value) : comment.ModifiedOn;
            ModifiedBy = comment.ModifiedBy;
            Post = comment.Post;
            PostId = comment.Post.Id;
            Notifications = comment.Notifications;
        }
    }
}